import Vue from 'vue'
import Vuex from 'vuex'
import axios from "axios"
import createPersistedState from 'vuex-persistedstate'
Vue.use(Vuex)

export default new Vuex.Store({
  plugins: [createPersistedState()],
  state: {
    // token: localStorage.getItem('token') || ''
    token: '',
    userinfo: {}
  },
  mutations: {
    updataToken (state, token) {
      state.token = token
      // localStorage.setItem('token', token)
    },
    updataUserInfo (state, payload) {
      state.userinfo = payload
    }
  },
  actions: {
    async getUserInfo (context) {
      const { data: res } = await axios('/my/userinfo')
      
      if (res.code !== 0) return

      context.commit('updataUserInfo', res.data)
    }
  },
})
